package com.example.project7.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.example.project7.R;
import com.example.project7.adapter.ListAdapter;
import com.example.project7.model.ListItem;
import com.example.project7.viewmodel.ListViewModel;
import java.util.List;

public class HomeFragment extends Fragment {
    private TextView tvRecommendation;
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView recyclerView;
    private ListAdapter listAdapter;
    private ListViewModel listViewModel;
    private String interest = "未选择";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        
        // 初始化控件
        tvRecommendation = view.findViewById(R.id.tv_recommendation);
        swipeRefresh = view.findViewById(R.id.swipe_refresh);
        recyclerView = view.findViewById(R.id.recycler_view);
        
        // 初始化RecyclerView
        initRecyclerView();
        
        // 初始化ViewModel
        initViewModel();
        
        // 设置下拉刷新监听
        setupSwipeRefresh();
        
        updateRecommendation();
        return view;
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        listAdapter = new ListAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(listAdapter);
        
        // 设置点击事件
        listAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, ListItem item) {
                // 跳转到详情页
                if (getActivity() != null) {
                    ((OnListItemClickListener) getActivity()).onListItemClick(item);
                }
            }
        });
        
        // 设置长按事件
        listAdapter.setOnItemLongClickListener(new ListAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position, ListItem item) {
                // 删除项
                listViewModel.deleteItem(item);
                return true;
            }
        });
    }

    /**
     * 初始化ViewModel
     */
    private void initViewModel() {
        listViewModel = new ViewModelProvider(this).get(ListViewModel.class);
        
        // 观察数据变化
        listViewModel.getItemList().observe(getViewLifecycleOwner(), new androidx.lifecycle.Observer<List<ListItem>>() {
            @Override
            public void onChanged(List<ListItem> listItems) {
                listAdapter.updateData(listItems);
            }
        });
    }

    /**
     * 设置下拉刷新监听
     */
    private void setupSwipeRefresh() {
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 刷新数据
                listViewModel.refreshData();
                
                // 2秒后停止刷新动画
                swipeRefresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (swipeRefresh != null) {
                            swipeRefresh.setRefreshing(false);
                        }
                    }
                }, 2000);
            }
        });
    }

    /**
     * 更新推荐文本
     */
    public void updateInterest(String newInterest) {
        this.interest = newInterest;
        if (tvRecommendation != null) {
            updateRecommendation();
        }
    }

    /**
     * 更新推荐文本显示
     */
    private void updateRecommendation() {
        tvRecommendation.setText("根据您的兴趣【" + interest + "】，推荐内容如下");
    }
    
    /**
     * 列表项点击回调接口
     */
    public interface OnListItemClickListener {
        void onListItemClick(ListItem item);
    }
}